from django.shortcuts import render,redirect,reverse,HttpResponse
from django.views import View
from .models import *
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
# Create your views here.
from .models import *
from django.contrib import auth
from django.contrib.auth.decorators import login_required
class Index(View):
    def get(self,request):
        date=Guest.objects.all()
        paginss=Paginator(date,2)
        page=request.GET.get('page')
        try:
            contac=paginss.page(page)
        except PageNotAnInteger:
            contac=paginss.page(1)
        except EmptyPage:
            contac=paginss.page(paginss.num_pages)
        return render(request,'index.html',{'guests':contac})

class Indexs(View):
    def get(self,request):
        date=Event.objects.all()
        pagin=Paginator(date,2)
        page=request.GET.get('page')
        try:
            contonc=pagin.page(page)
        except PageNotAnInteger:
            contonc=pagin.page(1)
        except EmptyPage:
            contonc=pagin.page(pagin.num_pages)
        return render(request,'Event_list.html',{'guests':contonc})

class Del(View):
    def get(self,request,id):
        Guest.objects.filter(id=id).delete()
        return redirect(reverse('app:index'))

class Dels(View):
    def get(self,request):
        date=Event.objects.all()
        return render(request,'add.html',{'date':date})

class Adds(View):
    def get(self, request):
        date = Guest.objects.all()
        return render(request, 'adds.html', {'date': date})
    def post(self,request):
        name=request.POST.get('name')
        limit=request.POST.get('limit')
        startus=request.POST.get('startus')
        address=request.POST.get('address')
        start_time=request.POST.get('start_time')
        create_time=request.POST.get('create_time')
        Guest.objects.create(name=name,limit=limit,startus=startus,address=address,start_time=start_time,create_time=create_time)
        return redirect(reverse('app:index'))
class Add(View):
    def get(self, request):
        date = Guest.objects.all()
        return render(request, 'adds.html', {'date': date})
    def post(self,request):
        event= request.POST.get('event')
        realname=request.POST.get('realname')
        phone=request.POST.get('phone')
        email=request.POST.get('email')
        sing=request.POST.get('sing')
        create_time=request.POST.get('create_time')
        Event.objects.create(event=event,realname=realname,phone=phone,email=email,sing=sing,create_time=create_time)
        return redirect(reverse('app:index'))

class Update(View):
    def get(self,request,u_id):
        date=Event.objects.get(id=u_id)
        list=Guest.objects.all()
        return render(request,'update.html',{'date':date,'list':list})
    def post(self,request,u_id):
        id=request.POST.get('id')
        event = request.POST.get('event')
        realname = request.POST.get('realname')
        phone = request.POST.get('phone')
        email = request.POST.get('email')
        sing = request.POST.get('sing')
        create_time = request.POST.get('create_time')
        Guest.objects.filter(id=id).update(event=event,realname=realname,phone=phone,email=email,sing=sing,create_time=create_time)
        return redirect(reverse('app:index'))

class Updates(View):
    def get(self,request,u_id):
        date=Event.objects.get(id=u_id)
        list=Guest.objects.all()
        return render(request,'updates.html',{'date':date,'list':list})
    def post(self,request,u_id):
        name = request.POST.get('name')
        limit = request.POST.get('limit')
        startus = request.POST.get('startus')
        address = request.POST.get('address')
        start_time = request.POST.get('start_time')
        create_time = request.POST.get('create_time')
        Guest.objects.filter(id=id).update(name=name,limit=limit,startus=startus,address=address,start_time=start_time,create_time=create_time)
        return redirect(reverse('app:index'))

class login(View):
    def get(self,request):
        return render(request,'login.html')
    def post(self,request):
        username=request.POST.get('username')
        password=request.POST.get('password')
        logins=auth.authenticate(username=username,password=password)
        if logins is not None:
            auth.login(request,logins)
            rep=redirect(reverse('app:index'))
            rep.set_cookie('username',username,3600)
            return rep
        else:
            return HttpResponse('密码错误')

# def sign_action(request,id):
#     date=Guest.objects.get(id=id)
#     if request.method=='POST':
#         phone=request.POST.get('phone')
#         result=Guest.objects.filter(phone=phone)
#         if not result:
#             return render(request,'sin.html',{'error':'phone错误','date':date})
#         result=Guest.objects.filter(phone=phone,event_id=id)
#         if not result:
#             return render(request,'sin.html',{'error':'phone或者event_id错误','date':date})
#         # result=Guest.objects.filter(phone=phone,event_id=id)
#         # if result.sign:
#         #     return render(request,'sign.html',{'error':'phone has sign in'})
#         else:
#             Guest.objects.filter(phone=phone,event_id=id).update(sing='1')
#             return render(request,'sin.html',{'error':'sign','Guest':result})
#     return render(request,'sin.html',{'date':date})

def tc(request):
    return redirect(reverse('app:index'))
def sign_action(request,eid):
    phone=request.POST.get('phone','')
    print(phone)
    result=Guest.objects.filter(phone=phone)
    if not result:
        return render(request,'sin.html',{'hint':'phone error'})
    result=Guest.objects.filter(phone=phone,event_id=eid)
    if not result:
        return render(request,'sin.html',{'hint':'event_id or phone error'})
    result=Guest.objects.get(phone=phone,event_id=eid)
    if result.sign:
        return render(request,'sin.html',{'hint':'user has sign in!!'})
    else:
        Guest.objects.get(phone=phone,event_id=eid).update(sign='1')
        return render(request,'sin.html',{'hint':'user sign successful!!!','guest':result})
